package com.dxq.wan.fragment;

import android.database.Observable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.dxq.basic_ui_library.loadSirCallBack.EmptyCallback;
import com.dxq.basic_ui_library.loadSirCallBack.ErrorCallback;
import com.dxq.basic_ui_library.loadSirCallBack.LoadingCallback;
import com.dxq.wan.R;
import com.dxq.wan.bean.LoginResult;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

/**
 * @author zsh
 * @date 2020/3/9.
 * 功能：
 */

public class Tab1Fragment extends BaseFragment {

    private SwipeRefreshLayout swipeRefreshLayout;
    private TextView tvContent;
    private String mTitle;

    //调用该方法，创建fragment
    public static Tab1Fragment newInstance(String title) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_KEY_TITLE, title);
        Tab1Fragment tab5Fragment = new Tab1Fragment();
        tab5Fragment.setArguments(bundle);
        return tab5Fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        if (null != arguments) {
            mTitle = arguments.getString(BUNDLE_KEY_TITLE, "");
        }
    }

    @Override
    protected int onCreateFragmentView() {
        return R.layout.fragment_tab;
    }

    @Override
    protected void initView(View view) {
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        tvContent = (TextView) view.findViewById(R.id.tv_content);
        tvContent.setText(mTitle);
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadNet();
            }
        });
    }

    /**
     * 网络加载
     */
    @Override
    protected void loadNet() {
        EasyHttp.post("/user/login")
                .params("username", "zsh").params("password", "123456")
                .execute(new SimpleCallBack<LoginResult>() {
                    @Override
                    public void onStart() {
                        //开始请求
                    }

                    @Override
                    public void onCompleted() {
                        //请求完成
                    }

                    @Override
                    public void onError(ApiException e) {
                        //请求错误
                        swipeRefreshLayout.setRefreshing(false);
                        mBaseLoadService.showCallback(ErrorCallback.class);
                        //tvContent.setText(e.getMessage());
                    }

                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        //请求成功
                        swipeRefreshLayout.setRefreshing(false);
                        mBaseLoadService.showSuccess();
                        //tvContent.setText(loginResult);
                        //Toast.makeText(getContext(), "toast:"+loginResult.getData().getNickname(), Toast.LENGTH_SHORT).show();
                    }
                });
    }

    /**
     * 重新加载
     * @param v
     */
    @Override
    protected void onNetReload(View v) {
        mBaseLoadService.showCallback(LoadingCallback.class);
        loadNet();
    }
}
